#include "RenderTarget.hpp"

using namespace Typhoon;

RenderTarget::RenderTarget() {
	isValid = false;
	frameBuffer = 0;
}


RenderTarget::~RenderTarget() {

}

void RenderTarget::Initialize(int size) {

	//e.LoadExtensions();

	width = size;
	height = size;

	glGenTextures(1, &texture);
	glBindTexture(GL_TEXTURE_2D, texture);
	
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	//glTexParameteri(GL_TEXTURE_2D, GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);

	
	//frame buffer
	//e.glGenFramebuffersOES(1, &frameBuffer);
	//e.glBindFramebufferOES(GL_FRAMEBUFFER_OES, frameBuffer);
	//e.glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,GL_COLOR_ATTACHMENT0_OES,GL_TEXTURE_2D,texture,0);

	//GLenum status = e.glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
	//if(status != GL_FRAMEBUFFER_COMPLETE_OES) {
	//	isValid = false;
	//} else {
	//	isValid = true;
	//}

	//e.glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
}

void RenderTarget::Begin() {
	//e.glBindFramebufferOES(GL_FRAMEBUFFER_OES, frameBuffer);
}

void RenderTarget::End() {
	//e.glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
}

void RenderTarget::Free() {
	Texture::Free();
	if (frameBuffer) {
		//e.glDeleteFramebuffersOES(1, &frameBuffer);
		frameBuffer = 0;
	}
}